package com.yh.workbill.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.yh.workbill.App;
import com.yh.workbill.R;
import com.yh.workbill.base.BaseActivity;
import com.yh.workbill.databinding.ActivityMainBinding;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.Company;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.ui.count.CountFragment;
import com.yh.workbill.ui.home.HomeFragment;
import com.yh.workbill.ui.me.MeFragment;
import com.yh.workbill.ui.mvp.MainPresenter;
import com.yh.workbill.ui.mvp.MainView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import static com.yh.workbill.utils.Config.SP_USER;

public class MainActivity extends BaseActivity implements MainView {

    private ActivityMainBinding binding;
    private MainPresenter presenter;
    private UserInfoDao userInfoDao;
    private CompanyDao companyDao;
    private UnitCompanyDao unitCompanyDao;

    private HomeFragment homeFragment;
    private CountFragment countFragment;
    private MeFragment meFragment;


    //记录当前正在使用的fragment
    private Fragment isFragment;
    private BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    }

    @Override
    public void initJetPack(@Nullable Bundle savedInstanceState) {
        binding.setLifecycleOwner(this);
        presenter = new MainPresenter(this);
        this.getLifecycle().addObserver(presenter);
    }

    @Override
    public void initObserve(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initBaseSetting(@Nullable Bundle savedInstanceState) {
        userInfoDao = App.appDataBase.getUserInfoDao();
        companyDao = App.appDataBase.getCompanyDao();
        unitCompanyDao = App.appDataBase.getUnitCompanyDao();
        //初始化Fragment及底部导航栏
        initFragment(savedInstanceState);
        bottomNavigationView = findViewById(R.id.nav_view);
    }

    @Override
    public void initClick(@Nullable Bundle savedInstanceState) {
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    @Override
    public void initDefaultMethod(@Nullable Bundle savedInstanceState) {
        SharedPreferences sp = getSharedPreferences(SP_USER, MODE_PRIVATE);
        String phone = sp.getString("phone", "");
        if (phone != null && !phone.equals("")) {
            ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
                UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
                String uuid = singleUserInfo.getUuid();
                //加载公司数据
                presenter.getCompanyData(companyDao, uuid);
                //加载使用单位数据
                presenter.getUseCompanyData(unitCompanyDao, uuid);
            });

        }
    }

    //https://blog.csdn.net/weixin_40804043/article/details/86744096
    public void initFragment(Bundle savedInstanceState) {
        //判断activity是否重建，如果不是，则不需要重新建立fragment.
        if (savedInstanceState == null) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
            }
            isFragment = homeFragment;
            ft.replace(R.id.container, homeFragment).commit();
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (homeFragment == null) {
                        homeFragment = new HomeFragment();
                    }
                    switchContent(isFragment, homeFragment);
                    return true;
                case R.id.navigation_count:
                    if (countFragment == null) {
                        countFragment = new CountFragment();
                    }
                    switchContent(isFragment, countFragment);
                    return true;
                case R.id.navigation_me:
                    if (meFragment == null) {
                        meFragment = new MeFragment();
                    }
                    switchContent(isFragment, meFragment);
                    return true;
            }
            return false;
        }
    };

    private void switchContent(Fragment from, Fragment to) {
        if (isFragment != to) {
            isFragment = to;
            FragmentManager fm = getSupportFragmentManager();
            //添加渐隐渐现的动画
            FragmentTransaction ft = fm.beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                ft.hide(from).add(R.id.container, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                ft.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }


    @Override
    public void getCompany(List<Company> companys) {

    }

    @Override
    public void getUseCompany(List<UseCompany> useCompanies) {

    }

    @Override
    public void showTasot(String message) {
        if (!message.equals("")) {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showDialog(String content, boolean isCancelable) {
        showLoaddingDialog(content, isCancelable);
    }

    @Override
    public void hideDialog() {
        hideLoaddingDialog();
    }
}
